package com.cskaoyan.javase.object._6clone._3exercise;

/**
 * FirstLevel 类有三个属性：
 * int firstIntValue;
 * double firstDoubleValue;
 * SecondLevel second;
 *
 * SecondLevel 类有三个属性：
 *
 * int secondIntValue;
 * double secondDoubleValue;
 * ThirdLevel third;
 *
 * ThirdLevel 类有两个属性：
 * int thirdIntValue;
 * double thirdDoubleValue;
 *
 * @since 17:31
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo {
    public static void main(String[] args) throws CloneNotSupportedException {
        First first = new First(10, 20, new Second(100, 200, new Third(1000, 2000)));
        First cloneFirst = first.clone();
    }
}

class First implements Cloneable{
    int firstIntValue;
    double firstDoubleValue;
    Second second;

    public First(int firstIntValue, double firstDoubleValue, Second second) {
        this.firstIntValue = firstIntValue;
        this.firstDoubleValue = firstDoubleValue;
        this.second = second;
    }

    @Override
    protected First clone() throws CloneNotSupportedException {
        // 1.深度克隆是在浅克隆的基础上完成的
        First cloneFirst = (First) super.clone();
        // 2.克隆一份对象中引用指向的对象
        // 需要克隆Second对象,于是对Second类做一系列操作
        Second cloneSecond = cloneFirst.second.clone();
        // 3.让克隆引用,指向克隆对象
        // 就是克隆second引用指向克隆second对象
        cloneFirst.second = cloneSecond;
        return cloneFirst;
    }
}

class Second implements Cloneable {
    int secondIntValue;
    double secondDoubleValue;
    Third third;

    public Second(int secondIntValue, double secondDoubleValue, Third third) {
        this.secondIntValue = secondIntValue;
        this.secondDoubleValue = secondDoubleValue;
        this.third = third;
    }

    @Override
    protected Second clone() throws CloneNotSupportedException {
        // 1.深度克隆是在浅克隆的基础上完成的
        Second cloneSecond = (Second) super.clone();
        // 2.克隆一份对象中引用指向的对象
        // 需要克隆一份third对象,所以需要对Third类做一系列操作
        Third cloneThird = third.clone();
        // 3.让克隆引用,指向克隆对象
        cloneSecond.third = cloneThird;
        return cloneSecond;
    }
}

class Third implements Cloneable{
    int thirdIntValue;
    double thirdDoubleValue;

    public Third(int thirdIntValue, double thirdDoubleValue) {
        this.thirdIntValue = thirdIntValue;
        this.thirdDoubleValue = thirdDoubleValue;
    }

    @Override
    protected Third clone() throws CloneNotSupportedException {
        return (Third) super.clone();
    }
}